﻿using NP.BPMReportPlatform.Entities.BPM_Business.CommonReportConfigs;
using NP.BPMReportPlatform.Reports.CommonReportConfigs.Dtos;
using NRules.Fluent.Dsl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NP.BPMReportPlatform.Reports.CommonReportConfigs
{
    public class DynamicRule : Rule
    {
        public override void Define()
        {
            RuleExecuteInput model = null;

            When()
                .Match(() => model, fact => EvaluateConditions(model));
            Then()
                .Do(ctx => ExecuteActions(model.rule.Message));
        }

        private bool EvaluateConditions(RuleExecuteInput model)
        {
            foreach (var fields in model.dataList)
            {
                // 动态解析并验证条件
                foreach (var condition in model.rule.ConditionList)
                {
                    var field = fields.FirstOrDefault(x => x.FieldName == condition.Field);
                    if (field == null || field.Field == null) throw new InvalidOperationException($"Property '{condition.Field}' not found.");

                    var value = CommonFieldHelper.GetTrueValueFromDbType(field.Field.DATA_TYPE, field.FieldValue, field.Field.COLUMN_DEFAULT);
                    var conditionValue = CommonFieldHelper.GetTrueValueFromDbType(field.Field.DATA_TYPE, condition.Value, field.Field.COLUMN_DEFAULT);
                    if (!EvaluateCondition(value, condition.Operator, conditionValue))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        private bool EvaluateCondition(object fieldValue, string operatorStr, object value)
        {
            switch (operatorStr)
            {
                case "=":
                    if ((fieldValue == null || fieldValue == DBNull.Value) || (value == null || value == DBNull.Value))
                    {
                        return true;
                    }
                    else
                    {
                        return Equals(fieldValue, value);
                    }
                case "!=":
                    if ((fieldValue == null || fieldValue == DBNull.Value) || (value == null || value == DBNull.Value))
                    {
                        return false;
                    }
                    return !Equals(fieldValue, value);
                case ">":
                    return CompareObjects(fieldValue, value) == -1;
                case "<":
                    return CompareObjects(fieldValue, value) == 1;
                default:
                    throw new InvalidOperationException($"Unsupported operator: {operatorStr}");
            }
        }

        private static int CompareObjects(object obj1, object obj2)
        {
            if (obj1 == null && obj2 == null)
                return 0; // 两个都是null，认为它们相等
            if (obj1 == null)
                return -1; // obj1是null，认为它小于任何非null对象
            if (obj2 == null)
                return 1; // obj2是null，认为它大于任何非null对象

            Type type1 = obj1.GetType();
            Type type2 = obj2.GetType();

            if (type1 != type2)
                throw new ArgumentException("Objects are of different types");

            if (obj1 is IComparable comparableObj1)
            {
                return comparableObj1.CompareTo(obj2);
            }
            else
            {
                throw new ArgumentException("Objects are not comparable");
            }
        }

        private void ExecuteActions(string errorMsg)
        {
            throw new Exception(errorMsg);
        }
    }
}
